﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;

namespace ERP_RORZE.Tool.UserControls
{
    public partial class ucReportsList : DevExpress.XtraEditors.XtraUserControl
    {
        ReportHelper helper = new ReportHelper();
        DataTable dt = new DataTable();

        public ucReportsList()
        {
            InitializeComponent();
        }

        private void barBtFImportDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFImportDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FImportDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFImportDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFMonthlyImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFMonthlyImport.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FMonthlyImport);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFMonthlyImport.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtUseDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtUseDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FUseDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtUseDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFNewOldDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFNewOldDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FNewOldDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFNewOldDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFBorrowDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFBorrowDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FBorrowDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFBorrowDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFTotalMachineAtFactory_BorrowAtPresent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFTotalMachineAtFactory_BorrowAtPresent.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FTotalMachineAtFactory_BorrowAtPresent);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFTotalMachineAtFactory_BorrowAtPresent.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFTotalMachineAtFactory_Crosstab_Borrow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFTotalMachineAtFactory_Crosstab_Borrow.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FTotalMachineAtFactory_Crosstab_Borrow);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtFTotalMachineAtFactory_Crosstab_Borrow.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFTotalMachineAtFactory_Borrow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFTotalMachineAtFactory_Borrow.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FQTotalMachineAtFactory_Borrow);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFTotalMachineAtFactory_Borrow.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFLossDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFLossDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FLossDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtFLossDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFTonKhoTongHop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFTonKhoTongHop.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FTonKhoTongHop);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtFTonKhoTongHop.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQTotalInstock1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQTotalInstock1.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QTotalInstock1);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtQTotalInstock1.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQTotalInStockAtPresent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQTotalInStockAtPresent.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QTotalInStockAtPresent);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtQTotalInStockAtPresent.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyInStock_Query_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //barStaticItem1.Caption = barBtQMonthlyInStock_Query.Caption;
            //gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyInStock_Query);
            //gridView1.PopulateColumns();
            //gridView1.OptionsView.ColumnAutoWidth = true;
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //barStaticItem1.Caption = barBtFMonthlyImport.Caption;
            //gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FMonthlyImport);
            //gridView1.PopulateColumns();
            //gridView1.OptionsView.ColumnAutoWidth = true;
        }

        private void barBtFDailyBroken_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFDailyBroken.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FDailyBroken);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtFDailyBroken.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken2_CrosstabAmount_USD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken2_CrosstabAmount_USD.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken2_CrosstabAmount_USD);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken2_CrosstabAmount_USD.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken2_CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken2_CrosstabQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken2_CrosstabQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken2_CrosstabQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Machine_CrosstabAmount_USD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Machine_CrosstabAmount_USD.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Machine_CrosstabAmount_USD);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Machine_CrosstabAmount_USD.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Machine_CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Machine_CrosstabQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Machine_CrosstabQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Machine_CrosstabQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Customer2_CrosstabAmount_USD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Customer2_CrosstabAmount_USD.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Customer2_CrosstabAmount_USD);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Customer2_CrosstabAmount_USD.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken3_CrosstabToolGroupAmount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken3_CrosstabToolGroupAmount.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken3_CrosstabToolGroupAmount);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken3_CrosstabToolGroupAmount.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken3_Crosstab_ToolGroupQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken3_Crosstab_ToolGroupQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken3_Crosstab_ToolGroupQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken3_Crosstab_ToolGroupQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Material2_CrosstabAmount_USD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Material2_CrosstabAmount_USD.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Material2_CrosstabAmount_USD);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Material2_CrosstabAmount_USD.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Material2_CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Material2_CrosstabQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Material2_CrosstabQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Material2_CrosstabQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Mistake2_CrosstabAmount_USD_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Mistake2_CrosstabAmount_USD.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Mistake2_CrosstabAmount_USD);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Mistake2_CrosstabAmount_USD.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Mistake2_CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Mistake2_CrosstabQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Mistake2_CrosstabQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Mistake2_CrosstabQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQTotalMachineBroken_Crosstab_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQTotalMachineBroken_Crosstab.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QTotalMachineBroken_Crosstab);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQTotalMachineBroken_Crosstab.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtQMonthlyBroken_Customer2_CrosstabQty_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtQMonthlyBroken_Customer2_CrosstabQty.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.QMonthlyBroken_Customer2_CrosstabQty);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ViewCaption = barBtQMonthlyBroken_Customer2_CrosstabQty.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtFReturnDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            barStaticItem1.Caption = barBtFReturnDetails.Caption;
            gridControl1.DataSource = helper.GetAll(ToolConstants.StoreProcedure.Report.FReturnDetails);
            gridView1.PopulateColumns();
            gridView1.OptionsView.ColumnAutoWidth = true;
            gridView1.ViewCaption = barBtFReturnDetails.Caption;
            Cursor.Current = Cursors.Default;
        }

        private void barBtExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.RowCount > 0)
            {
                XlsxExportOptions option = new XlsxExportOptions();
                option.ShowGridLines = true;
                option.SheetName = barStaticItem1.Caption;

                FileDialog fileDialog = new SaveFileDialog();
                fileDialog.Filter = "Excel 2007-2013|*.xlsx|Excel 97-2003 (*.xls)|*.xls";
                fileDialog.FileName = barStaticItem1.Caption;
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    //gridView1.OptionsView.ColumnAutoWidth = false;
                    gridView1.ExportToXlsx(fileDialog.FileName, option);
                }
            }
            else
            {
                MessageBox.Show(ToolConstants.Message.NoDataToExport, ToolConstants.Form.MessageBox.Message,
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
    }
}
